package controller;

import dao.BookRecordDao;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.BookRecord;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

@WebServlet(name = "AdminRecordServlet", value = "/admin/AdminRecord")
public class AdminRecordServlet extends HttpServlet {
    public static final Logger logger = LogManager.getLogger(AdminRecordServlet.class);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String method = request.getParameter("method") == null ? "" : request.getParameter("method");
        RequestDispatcher requestDispatcher;
        ServletContext servletContext = this.getServletContext();
        BookRecordDao bookRecordDao = new BookRecordDao();
        if ("list".equals(method)) {
            try {
                ArrayList<BookRecord> bookRecordArrayList = bookRecordDao.getAllRecordList();
                request.setAttribute("bookRecordList", bookRecordArrayList);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/deal/book_record_list.jsp");
            requestDispatcher.forward(request, response);
        }

        // 借书操作
        if ("borrowBook".equals(method)) {
            BookRecord bookRecord = (BookRecord) request.getAttribute("borrowBook");
            int bookId = bookRecord.getBookId();
            int userId = bookRecord.getUserId();
            logger.info("borrowBook bookId = " + bookId + " userId = " + userId);
            Date date = new Date();
            try {
                if (bookRecordDao.borrowBook(bookId, userId, date)) {
                    request.getSession().setAttribute("borrowBook_message", "借书成功");
                } else {
                    request.getSession().setAttribute("borrowBook_message", "借书失败");
                }
                response.sendRedirect("/admin/book_record_list.jsp");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        // 还书操作
        if ("backBook".equals(method)) {
            int id = Integer.parseInt(request.getParameter("id"));
            logger.info("backBook id = " + id);
            Date date = new Date();
            try {
                if (bookRecordDao.backBook(date, id)) {
                    request.getSession().setAttribute("borrowBook_message", "还书成功");
                } else {
                    request.getSession().setAttribute("borrowBook_message", "还书失败");
                }
                response.sendRedirect("/admin/book_record_list.jsp");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
